import {
  Component,
  OnInit,
  EventEmitter,
  ViewEncapsulation,
  ChangeDetectionStrategy,
  OnChanges
} from '@angular/core';
import { QuoteHeader } from "quote";


@Component({
  // moduleId: module.id,
  selector: 'quote-table-row',
  templateUrl: 'quote-table-row.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush, // 要内挂其他模块，取消OnPush策略，其实影响不大
  encapsulation: ViewEncapsulation.None,
  inputs: [
    "headers",
    "quote",
    "isHeaderRow",
    "isEmptyRow",
    "currentSort",
    "appendModule"
  ],
  outputs: [
    "quoteClick",
    "quoteDblClick"
  ]
})
export class QuoteTableRowComponent implements OnInit, OnChanges {
  quoteClick = new EventEmitter();
  quoteDblClick = new EventEmitter();
  quote;
  currentSort;
  isHeaderRow;
  isEmptyRow;
  headers;

  constructor() { }

  ngOnInit() {

  }

  getSuitableClass(params: QuoteHeader, value, vChangeRate) {
    if ((value === undefined || value === "") && !params.appendModule) return {};
    // 现价
    if (params.key === 'current') {
      return Object.assign({}, {
        "text-go-up": params.withUpOrDown && vChangeRate > 0,
        "text-go-down": params.withUpOrDown && vChangeRate < 0,
      }, this.getHeaderClass(params));
    } else {
      if (params.withUp) {
        // 委比排行
        return Object.assign({}, {
          "text-go-up": params.withUp && value > 0,
        }, this.getHeaderClass(params));
      } else {
        return Object.assign({}, {
          "text-go-up": params.withUpOrDown && value > 0,
          "text-go-down": params.withUpOrDown && value < 0,
          // "text-percent": params.withPercent
        }, this.getHeaderClass(params));
      }
    }
  }

  getHeaderClass(params: QuoteHeader, isHeaderRow?) {
    let appendClass, activeClass, appendModule;
    appendClass = params.class ? params.class.reduce((acc, cur) => Object.assign({}, acc, { [cur]: true }), {}) : {};
    activeClass = { "active": this.currentSort === params.key };
    appendModule = params.appendModule ? { "append-module": true } : {};
    return Object.assign({}, appendClass, activeClass, appendModule);
  }

  ngOnChanges(change) {
    // console.debug("Change: ", change);
    if (change["quote"]) {
      this.quote = change["quote"]["currentValue"];
    }
    // if (change["headers"] && Object.is(change["quote"]["currentValue"], this.headers)) {
    //   this.headers = change["headers"]["currentValue"];
    // }
  }



  onClick(quote, valid) {
    // console.debug("Table Row / Cell Click: ", quote, valid);
    // if (!quote.noSort) return;
    this.quoteClick.next(quote);
  }

  onDblClick(quote) {
    // console.debug("quote Db Click From Row: ", quote);
    this.quoteDblClick.next(quote);
  }

}